package com.example.userserver.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.userserver.entity.UserInfo;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.enums.UserStatusEnum;
import com.example.userserver.manager.AccountManage;
import com.example.userserver.params.LoginParams;
import com.example.userserver.params.RegisterParams;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.utils.Md5Utils;
import com.example.userserver.vo.LoginVO;
import com.example.userserver.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AccountService {
    @Autowired
    AccountManage accountManage;
    public Result<LoginVO> login(LoginParams loginParams) {
        UserInfo userInfo = accountManage.getUserInfoByAccount(loginParams.getAccount());
        if(ObjectUtil.isNotNull(userInfo)){
            // 账号已注销
            if(userInfo.getStatus() == UserStatusEnum.CANCEL.getCode()){
                return new Result(null, ReturnEnum.ACCOUNT_CANCEL);
            }
            // 校验密码
            if(checkPassword(loginParams.getPassword(), userInfo.getPassword(), userInfo.getSalt())){
                String token = accountManage.getNewToken(loginParams.getAccount());
                LoginVO loginVO = new LoginVO(userInfo.getId(), userInfo.getUserName(), token, userInfo.getPhoto(), userInfo.getRole());
                return new Result(loginVO, ReturnEnum.SUCCESS);
            }
        }
        return new Result(null, ReturnEnum.LOGIN_WRONG);
    }

    public Boolean checkPassword(String inputPassword, String truePassword, String salt) {
        String hashedPassword = getHashedPassword(inputPassword, salt);
        if(!StringUtils.isEmpty(hashedPassword)){
            if(hashedPassword.equals(truePassword)) {
                return true;
            }
        }
        return false;
    }

    private String getHashedPassword(String password, String salt) {
        return Md5Utils.generateMd5(String.format("%s%s", password, salt));
    }

    public Result<LoginVO> register(RegisterParams registerParams){
        if(accountManage.checkAccount(registerParams.getAccount())){
            return new Result(null, ReturnEnum.ACCOUNT_RE);
        }
        if(accountManage.checkUserName(registerParams.getUserName())){
            return new Result(null, ReturnEnum.USERNAME_RE);
        }
        UserInfo userInfo = new UserInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(registerParams, userInfo);
        String salt = Md5Utils.getRandom32Str();
        String hashedPassword = getHashedPassword(registerParams.getPassword(), salt);
        userInfo.setSalt(salt);
        userInfo.setPassword(hashedPassword);
        accountManage.register(userInfo);
        String token = accountManage.getNewToken(registerParams.getAccount());
        LoginVO loginVO = new LoginVO(userInfo.getId(), userInfo.getUserName(), token, userInfo.getPhoto(), userInfo.getRole());
        return new Result(loginVO, ReturnEnum.SUCCESS);
    }

    public Boolean checkToken(String token){
        return accountManage.checkToken(token);
    }

    public String getUserIdByToken(String token){
        return accountManage.getUserIdByToken(token);
    }

    public Result checkFreeze(String id) {
        UserInfo userInfo = accountManage.getUserById(id);
        // 账号冻结
        if(userInfo.getStatus() == UserStatusEnum.FREEZE.getCode()){
            return new Result(null, ReturnEnum.ACCOUNT_FREEZE);
        }
        return new Result(null, ReturnEnum.SUCCESS);
    }
}
